package com.aykls.service.impl;

import com.aykls.Vo.Enroll;
import com.aykls.Vo.ResultVo;
import com.aykls.entity.Admin;
import com.aykls.mapper.AdminMapper;
import com.aykls.service.AdminService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author aykls
 * @since 2022-11-19
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    @Autowired
    AdminMapper adminMapper;
    @Override
    public ResultVo Login(Admin a) {
        ResultVo resultVo = new ResultVo();
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",a.getMobile());
        Admin admin = adminMapper.selectOne(queryWrapper);
        if(admin != null){
            if(admin.getPassword().equals(a.getPassword())){
                resultVo.setCode(0);
                resultVo.setMessage("登入成功");
                resultVo.setSuccess(true);

            }else{
                resultVo.setSuccess(false);
                resultVo.setCode(1);
                resultVo.setMessage("密码错误");
            }
        }else{
            resultVo.setMessage("电话号码错误");
            resultVo.setCode(1);
            resultVo.setSuccess(false);
        }
        return resultVo;
    }

    @Override
    public ResultVo enroll(Enroll enroll) {
        ResultVo resultVo =new ResultVo();
        QueryWrapper queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("mobile",enroll.getMobile());
        Admin admin = adminMapper.selectOne(queryWrapper);
        if(admin!=null){
            resultVo.setSuccess(false);
            resultVo.setCode(1);
            resultVo.setMessage("用户名已存在");
        }else{
            if(enroll.getPassWord().equals(enroll.getRePassword())){
                Admin admin1 = new Admin(enroll.getMobile(),enroll.getPassWord());
                adminMapper.insert(admin1);
                resultVo.setSuccess(true);
                resultVo.setCode(0);
                resultVo.setMessage("创建成功");
            }else{
                resultVo.setMessage("密码不一致");
                resultVo.setCode(1);
                resultVo.setSuccess(false);
            }
        }
        return resultVo;
    }
}
